package org.mspring.mlog.web.admin;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.mspring.mlog.common.exception.OperateException;
import org.mspring.mlog.web.BaseAdminController;
import org.mspring.mlog.entity.TagEntity;
import org.mspring.mlog.entity.UserEntity;
import org.mspring.mlog.service.TagEntityService;
import org.mspring.mlog.support.TabItem;
import org.mspring.nbee.common.PageResult;
import org.mspring.nbee.db.dao.sql.builder.part.condition.Operator;
import org.mspring.nbee.db.dao.sql.builder.part.condition.SqlCondition;
import org.mspring.nbee.db.dao.sql.pager.OrderType;
import org.mspring.nbee.db.dao.sql.pager.SqlPager;
import org.mspring.nbee.web.support.QueryParam;
import org.mspring.nbee.web.support.WebContext;
import org.mspring.nbee.web.view.json.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @author Gao Youbo
 * @since 2015-01-02 15:54
 */
@Controller
@RequestMapping("/admin/tag")
@TabItem(item = "100115")
public class TagController extends BaseAdminController {
    private TagEntityService tagEntityService;

    @RequestMapping("/list")
    @TabItem(item = "100115005")
    public String list(WebContext context, Model model) {
        SqlPager pager = context.getPager();
        pager.addOrder("id", OrderType.DESC);
        List<SqlCondition> conditions = context.buildSqlConditions(
                model,
                new QueryParam(TagEntity.Fields.name, Operator.LIKE)
        );
        PageResult<TagEntity> result = tagEntityService.list(conditions, pager);
        model.addAttribute("result", result);
        return "/admin/tag/list";
    }

    @RequestMapping("/create")
    @TabItem(item = "100115010")
    public String create(WebContext context, Model model) {
        return "/admin/tag/create";
    }

    @RequestMapping("/create/do")
    public String doCreate(WebContext context, Model model) {
        UserEntity currentUser = getCurrentUser();
        TagEntity tag = context.fromRequest(TagEntity.class);
        tag.setCreateTime(new Date());
        tag.setCreateUser(currentUser.getId());
        tag.setCreateUserName(currentUser.getNickname());
        tag.setUpdateTime(new Date());
        tag.setUpdateUser(currentUser.getId());
        tag.setUpdateUserName(currentUser.getNickname());
        tagEntityService.insert(tag);
        return "redirect:/admin/tag/list";
    }

    @RequestMapping("/edit")
    @TabItem(item = "100115015")
    public String edit(WebContext context, Model model) {
        Long id = context.getParamAsLong("id");
        if (id == null) {
            throw new OperateException("操作异常：请选择要编辑的分类");
        }
        TagEntity tag = tagEntityService.findById(id);
        model.addAttribute("tag", tag);
        return "/admin/tag/edit";
    }

    @RequestMapping("/edit/do")
    public String doEdit(WebContext context, Model model) {
        UserEntity currentUser = getCurrentUser();
        long id = context.getRequiredLong("id");
        TagEntity tag = tagEntityService.findById(id);
        tag = context.fromRequest(tag);
        tag.setUpdateTime(new Date());
        tag.setUpdateUser(currentUser.getId());
        tag.setUpdateUserName(currentUser.getNickname());
        tagEntityService.update(tag);
        return "redirect:/admin/tag/list";
    }

    @RequestMapping("/delete/do")
    @ResponseBody
    public JsonResult delete(WebContext context, Model model) {
        long id = context.getRequiredLong("id");
        boolean flag = tagEntityService.deleteById(id);
        return JsonResult.getJsonResult(flag);
    }

    @RequestMapping("/autocomplete")
    @ResponseBody
    public JsonResult autocomplete(WebContext context) {
        String name = context.getParam("name");
        List<TagEntity> tags = Lists.newArrayList();
        if (StringUtils.isNotBlank(name)) {
            tags = tagEntityService.listByName(name);
        }
        return JsonResult.getJsonResult(tags);
    }

    @Autowired
    public void setTagEntityService(TagEntityService tagEntityService) {
        this.tagEntityService = tagEntityService;
    }
}
